* xtsccfixedb_tab.ado
program xtsccfixedb_tab, eclass
        syntax [, level(int `c(level)') ]
        tempname mytab z t p ll ul
        .`mytab' = ._tab.new, col(7) lmargin(0)
        .`mytab'.width    13   |12    12     8     8    12    12
        .`mytab'.titlefmt  .     .     .   %6s     .  %24s     .
        .`mytab'.pad       .     2     1     0     2     3     3
        .`mytab'.numfmt    . %9.0g %9.0g %7.2f %5.3f %9.0g %9.0g

        local namelist : colname e(b)
        local eqlist : coleq e(b)
        local k : word count `namelist'
        .`mytab'.sep, top
        if `:word count `e(depvar)'' == 1 {
                local depvar "`e(depvar)'"
        }
        .`mytab'.titles "`depvar'"                      /// 1
                        "Coef."                         /// 2
				"Std. Err."                     /// 3
                        "t"                        /// 4
                        "P>|t|"                    /// 5
                        "[`level'% Conf. Interval]" ""  //  6 7
        forvalues i = 1/`k' {
                local name : word `i' of `namelist'
                local eq   : word `i' of `eqlist'
                if "`eq'" != "_" {
                        if "`eq'" != "`eq0'" {
                                .`mytab'.sep
                                local eq0 `"`eq'"'
                                .`mytab'.strcolor result  .  .  .  .  .  .
                                .`mytab'.strfmt    %-12s  .  .  .  .  .  .
                                .`mytab'.row      "`eq'" "" "" "" "" "" ""
                                .`mytab'.strcolor   text  .  .  .  .  .  .
                                .`mytab'.strfmt     %12s  .  .  .  .  .  .
                        }
                        local beq "[`eq']"
						ereturn local vcetype "`Fixed-b'"
                }
                else if `i' == 1 {
                        local eq
                        .`mytab'.sep
                }

                scalar `t' = `beq'_b[`name']/`beq'_se[`name']

             * map the t-statistic to the z-value using the inverse fixed-b mapping: stored in fixedbz
                local tstat = `t'
                mata: ttoz(`e(fixedb)',`tstat')      
                scalar `p' = 2*normal(-abs(fixedbz))
                scalar `ll' = `beq'_b[`name']-`beq'_se[`name']*cvtfixedb
                scalar `ul' = `beq'_b[`name']+`beq'_se[`name']*cvtfixedb

                .`mytab'.row    "`name'"                ///
                                `beq'_b[`name']         ///
                                `beq'_se[`name']        ///
                                `t'                 ///
                                `p'                     ///
                                `ll' `ul'
        }
        .`mytab'.sep, bottom
end

* Given the value of fixedb, this function maps the absolute value of t-statistics to
* a the absolute value of a z-value so that fixed-b p-values can be computed using a normal distribution


mata void ttoz(real scalar b, real scalar y)
{
        real scalar gamma, theta, x


if (b<=0.08){	
gamma =  b*0.767 + b*b* -10.864 + b*b*b*  72.261
theta =  b*   0.287 + b*b*   6.576 + b*b*b* -42.401
}

if (b>0.08){	
gamma =  b*0.455 + b*b*  -1.617 + b*b*b*   5.314 
theta =  b*0.459 + b*b*    1.289 + b*b*b*  -3.083
}

if (b>0.2){	 
gamma =  b*0.422 + b*b*   -0.650 + b*b*b*   1.302 
theta =  b*0.439 + b*b*    1.071 + b*b*b*  -1.510
}

if (b>0.5){	
gamma =  b*0.033 + b*b* 0.957 + b*b*b*  -0.352
theta =  b*0.851 + b*b*   -0.579 + b*b*b*   0.145
}
	

        if (b==0) {
          x=abs(y)
        }
        if (b>0) {
        x=(-(1+gamma)+sqrt(((1+gamma)*(1+gamma))+4*theta*(abs(y))))/(2*theta)
        }
        st_numscalar("fixedbz",x)
       
}




